<?php
require_once('../../libraryfiles/config.php');


$action = isset($_GET['action']) ? $_GET['action'] : '';

switch($action)
{
	case 'add' :
		add();
		break;
			
	case 'delete' :
		Delete();
		break;
	case 'delselected' :
		 MultiDelete();
		break;
	
	case 'publish':
		PublishUnpublish();
		break;
	default :
		header('Location:'.base_url_admin.'/vdoyoutube/index.php');
		exit();
}

function add(){
	try {
		$getPost = $_REQUEST;
		extract($getPost);
		$url=$txtvdoyoutubeid;
		if(empty($txtvdotitle)){
			header('Location:'.base_url_admin.'/vdoyoutube/index.php?view=add' . $link . '&error=' . urlencode('Video title are required!'));
			exit();
		}elseif(empty($txtvdoyoutubeid)){
			header('Location:'.base_url_admin.'/vdoyoutube/index.php?view=add' . $link . '&error=' . urlencode('Youtube ID are required!'));
			exit();
		}
		
		preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches);
		
		if(!empty($hidid)){
			$id = (int)$hidid;
			$Createdate = strtotime(date('d-m-Y h:m:s'));
			$SqlStatementUpdate = "UPDATE ".Tblvdoyoutube::tblvdoyoutube." SET ".Tblvdoyoutube::vdo_title."='".trim($txtvdotitle)."',".Tblvdoyoutube::vdo_ytb_id."='".trim($matches[0])."',".Tblvdoyoutube::vdo_status."=".$CboVdoStatus.",".Tblvdoyoutube::vdo_description."='".trim($txtdescription)."',".Tblvdoyoutube::use_id."=".$_SESSION['acc_id'].",".Tblvdoyoutube::date_modified."='".$Createdate."' WHERE ".Tblvdoyoutube::id."=".$id;
			$SuccessUpdate = mysql_query($SqlStatementUpdate);
			if($SuccessUpdate){
				header('Location:'.base_url_admin.'/vdoyoutube/index.php?success='. urlencode('Update successfully'));
			}else{
				header('Location:'.base_url_admin.'/vdoyoutube/index.php?view=add' . $link . '&error=' . urlencode('Error update'));
				exit();
			}
		}else{
			$Createdate = strtotime(date('d-m-Y h:m:s'));
			$SqlStatementInsert = "INSERT INTO ".Tblvdoyoutube::tblvdoyoutube." (".Tblvdoyoutube::vdo_title.",".Tblvdoyoutube::vdo_ytb_id.",".Tblvdoyoutube::vdo_status.",".Tblvdoyoutube::vdo_description.",".Tblvdoyoutube::use_id.",".Tblvdoyoutube::date_created.") VALUES('".trim($txtvdotitle)."','".trim($matches[0])."','".$CboVdoStatus."','".trim($txtdescription)."','".$_SESSION['acc_id']."','".$Createdate."')";
			$SuccessInsert = mysql_query($SqlStatementInsert);
			if($SuccessInsert){
				header('Location:'.base_url_admin.'/vdoyoutube/index.php?success='. urlencode('Add successfully'));
				exit();
			}else{
				header('Location:'.base_url_admin.'/vdoyoutube/index.php?view=add' . $link . '&error=' . urlencode('Error Insert'));
				exit();
			}
		}
	}catch (Exception $e){
		header('Location:'.base_url_admin.'/vdoyoutube/index.php?view=add' . $link . '&error=' . urlencode($e->getMessage()));
		exit();
	}
}

function Delete(){
	try{
		$id = (int)$_GET['id'];
		$sql = "DELETE FROM ".Tblvdoyoutube::tblvdoyoutube ." WHERE ".Tblvdoyoutube::id ."=".$id;
		$deleteSucess = mysql_query($sql);
		if($deleteSucess){
			header('Location:'.base_url_admin.'/vdoyoutube/index.php?success='. urlencode('Delete successfully'));
			exit();
		}else{
			header('Location:'.base_url_admin.'/vdoyoutube/index.php?error=' . urlencode('Error Insert'));
			exit();
		}

	}catch (Exception $e){
		header('Location:'.base_url_admin.'/vdoyoutube/index.php?error=' . urlencode($e->getMessage()));
		exit();
	}
}

function MultiDelete(){
	try {
		if(isset($_POST['id'])){
			$id = (is_array($_POST['id']))?implode(',',$_POST['id']):$_POST['id'];
			$sql = "DELETE FROM ".Tblvdoyoutube::tblvdoyoutube." WHERE ".Tblvdoyoutube::id." IN ($id)";
			mysql_query($sql);
		}
			header('Location: ' . $_SESSION['shop_return_url']);
	} catch (Exception $e) {
	}
}

function PublishUnpublish(){
	
	if(isset($_POST['id'])){
		if(isset($_GET['publish']) && (int)$_GET['publish']>=0 && (int)$_GET['publish']<=1){
			$status = (int)$_GET['publish'];
		}
		else{
			header('Location:'.base_url_admin.'/vdoyoutube/index.php');
			exit();
		}
		$id = (is_array($_POST['id']))?implode(',',$_POST['id']):$_POST['id'];

		$sql_pub = "UPDATE ".Tblvdoyoutube::tblvdoyoutube." SET ".Tblvdoyoutube::vdo_status."=".$status." WHERE id IN ($id)";
		mysql_query($sql_pub);
	}
	
	header('Location:'.base_url_admin.'/vdoyoutube/index.php?'.getAllGetParams(array('view','action','publish')));
	exit();
	
}

?>